package com.example.petstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.petstore.entity.Collection;
import com.example.petstore.entity.Product;
import com.example.petstore.entity.VO.CollectionVO;
import com.example.petstore.entity.VO.PageVO;
import com.example.petstore.entity.VO.ProductVO;
import com.example.petstore.mapper.ProductMapper;
import com.example.petstore.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.petstore.utils.Result;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DD
 * @since 2024-07-04
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Override
    public Result getAll(PageVO pageVO) {

        Page<Product> productPage = new Page<>(pageVO.getCurrent(),pageVO.getSize());
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Product::getPetType,pageVO.getSearch());
        this.page(productPage,queryWrapper);

        List<Product> records = productPage.getRecords();
        ProductVO productVO = new ProductVO();
        productVO.setList(records);

        return Result.success(productVO);
    }

    @Override
    public Result getOne(int productId) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getId,productId);

        Product one = this.getOne(queryWrapper);
        return Result.success(one);
    }
}
